package com.ody.util.code.model;

import com.google.common.collect.Lists;

import java.util.List;

public class TablePatternParser extends PatternParser {
    private List<Rel> oneToOneList;
    private List<Rel> oneToManyList;
    
    public TablePatternParser(String pattern, String name) {
        super(pattern, name);
    }
    
    public List<Rel> getOneToOneList() {
        return oneToOneList;
    }
    
    public List<Rel> getOneToManyList() {
        return oneToManyList;
    }
    
    @Override
    protected void addPattern(String pattern) {
        if (oneToManyList == null) {
            oneToOneList = Lists.newArrayList();
        }
        if (oneToManyList == null) {
            oneToManyList = Lists.newArrayList();
        }
        if (pattern.contains("||")) {
            String[] rel = pattern.split("\\|\\|");
            String[] tk1 = rel[0].split("[:]");
            String[] tk2 = rel[1].split("[:]");
            oneToManyList.add(new Rel(tk1[0], tk1[1], tk2[0], tk2[1]));
    
            super.addPattern(tk1[0]);
        } else if (pattern.contains("|")) {
            String[] rel = pattern.split("\\|");
            String[] tk1 = rel[0].split("[:]");
            String[] tk2 = rel[1].split("[:]");
            oneToOneList.add(new Rel(tk1[0], tk1[1], tk2[0], tk2[1]));
    
            super.addPattern(tk1[0]);
        } else {
            super.addPattern(pattern);
        }
    }
    
    static class Rel {
        private String table;
        private String key;
        private String relTable;
        private String relKey;
        
        Rel(String table, String key, String relTable, String relKey) {
            this.table = table;
            this.key = key;
            this.relTable = relTable;
            this.relKey = relKey;
        }
    
        public String getTable() {
            return table;
        }
    
        public String getKey() {
            return key;
        }
    
        public String getRelTable() {
            return relTable;
        }
    
        public String getRelKey() {
            return relKey;
        }
    }
}
